<template>
  <!-- 方案规划导航栏 -->
   <div style="width: 100%;height: 100%;text-align: center;">

  
    <div id="elePriceDiv" style="display: flex;margin-top: 1vh;" class="box">
 
      <el-form :inline="true" :model="formInline" disabled="disabled" style="margin-left: 1vh;margin-top: 0.5vh;width: 76%;" class="demo-form-inline">
        <el-form-item label="计算状态:">
          <el-input style="width: 60%;margin-top: 0vh;" v-model="calculationStatusLabel">
          </el-input>
        </el-form-item>

        <el-form-item label="计算开始时间:">
          <el-input style="width: 90%;margin-top: 0vh;" v-model="form.calculationStartTime"></el-input>
        </el-form-item>

        <el-form-item label="计算结束时间:">
          <el-input style="width: 90%;margin-top: 0vh;" v-model="form.calculationEndTime"></el-input>
        </el-form-item>

        <el-form-item label="计算耗时:">
          <el-input style="width: 65%;margin-top: 0vh;" v-model="form.calculationTime"></el-input>
        </el-form-item>

      </el-form>

      <div id="buttonDiv" style="text-align: right;margin-top: 0.7vh;width: 24%;">

        <el-button  v-if="this.suspendCalculatFlag==1" type="primary" @click="suspendCalculat()" >
          <span class="button-text">中止计算</span>
        </el-button>

        <el-button type="primary"  v-if="form.calculationStatus!=2" @click="buttonClick()" >
          <span class="button-text">{{buttonText}}</span>
        </el-button>

        <el-button type="primary" v-if="form.calculationStatus==2" @click="executeAlgorithm()">
          <span class="button-text">计算</span>
        </el-button>

        <el-button type="primary" v-if="form.calculationStatus==2" @click="captureData()" >
          <span class="button-text">抓取数据</span>
        </el-button>
 
        <el-button type="primary" v-if="form.calculationStatus==2" @click="setModelData()" >
          <span class="button-text">设置模型误差数据</span>
        </el-button>

      </div>

       
    </div>
 
    
      
    <!-- 弹窗 -->
    <el-dialog title="配置模型误差" :visible.sync="dialogShowFlag" style="margin-top: 24vh;margin-left: 22vh;" width="45%" align="left">
      
      <div style="display: inline-flex;margin-top: -15px;width: 100%;">
        <img style="height: 24px;margin-top: 0.2vh;" src="../../assets/images/data.png">
        <div style="width: 70%;margin-left: 10px;margin-top:0px;font-family:emoji;color: #f2f2f2;font-size: 0.9vw;">本次算法模型计算平准化制氢成本<span style="color: #5de1ef;">{{ valueData }}</span>{{ valueUnit }}</div>
        
        
        <el-select title="基准设置：" style="float: right;margin-right: 1vh;" @change="typeChange()" v-model="basisType" placeholder="请选择对比基准">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>

                
        <el-select style="float: right;margin-right: 1vh;" v-model="rowValue" placeholder="请选择基准行">
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>

      </div>

      
      <div slot="footer">
        <el-button type="primary" @click="saveErrorModelData()">保存</el-button>
        <el-button type="primary" @click="closeDialog()">取消</el-button>
      </div>
    </el-dialog>
   

</div>
  
</template>

<script>
 

  export default {
    name: 'resourceParameters',
    data() {
      return {
        calculationStatusLabel : '',
        suspendCalculatFlag : "0",
        form: {
          id: '',
          
          calculationStatus: '',
          calculationStartTime: '',
          calculationEndTime: '',
          calculationTime: '',
          projectId: '',
        },
        activeDate: '',
        buttonText: '计算', 
        dialogShowFlag : false,
        options: [
          {
            value: '1',
            label: '对比基准1'
          },  
          {
            value: '2',
            label: '对比基准2'
          },
        ],
        basisType:"",
        rowValue:"",
        valueData : "-",
        valueUnit : "元/kg", 
        projectId : ""
      }
    },
    mounted: function() {
      //获取项目id
      this.projectId = this.$route.query.projectId;
      this.form.projectId = this.projectId;
      this.queryPageData();
    },
    methods: {

      typeChange:function () {
        if(this.basisType=="1"){
          this.options2 = [
            {
              value: '1',
              label: '第一行'
            },  
            {
              value: '2',
              label: '第二行'
            },
            {
              value: '3',
              label: '第三行'
            },
          ]
        }else{

          this.options2 = [
            {
              value: '1',
              label: '第一行'
            }
          ]

        }        
        this.rowValue = "1";
      },
      closeDialog : function(){
        this.dialogShowFlag = false;
      },

      setModelData:function(){

        
        var vm = this;

        this.$http.post('/iepsapi/ea/hpEconomicAnalysis/queryCostBreakdown?projectId='+this.projectId+"&tableName=hp_cost_breakdown").then((res) => {
          var result = res.data;
         
          if(result){
            vm.valueData = result.hpCost;
            vm.dialogShowFlag = true;
          }
        }).catch((error) => {
          console.log(error);
        });


       
      },

      saveErrorModelData : function(){

        if(this.basisType==null||this.basisType==""){
          this.$message({
            type: 'info',
            message: '请选择对比基准!'
          });
          return;
        }else if(this.rowValue==null||this.rowValue==""){
          this.$message({
            type: 'info',
            message: '请选择基准行!'
          });
          return;
        }

        var vm = this;
        this.$http.post('/iepsapi/ea/hpEconomicAnalysis/saveErrorModelData?projectId='+this.projectId+"&modelValue="+this.valueData+"&type=hpErrorModel&basisType="+this.basisType+"&rowValue="+this.rowValue, {
          "modelValue": vm.valueData, 
          "type": "hpErrorModel",
          "basisType": vm.basisType,
          "rowValue": vm.rowValue, 
        }).then((res) => {
        
          this.$message({
            type: 'success',
            message: '操作成功，请前往模型误差功能查看对比详情!'
          });
           
        });
      },

      closeDialog:function(){
        this.dialogShowFlag = false;
      },
      
      captureData: function(){
        
        this.$prompt('请输入生成的数据指标名称', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',  
        }).then(({ value }) => {

        var dataName = value;
        
          const loading = this.$loading({
            lock: true,
            text: '数据抓取中，请稍等',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });

          var url = "captureData";

          this.$http.post('/iepsapi/ieps/basicDatabase/'+url+'?dataName='+dataName+"&type=hydrogen").then((res) => {
            var successFlag = res.data.successFlag;
            if(successFlag=="success"){

              loading.close();

              this.$message({
                type: 'success',
                message: '操作成功，请前往基础数据库查看生成的数据!'
              });

            }else{
  
              this.$message({
                type: 'info',
                message: '抓取失败!'
              });

            }
          }).catch((error) => {
            console.log(error);
          });
          
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });       
        });

      },


      queryPageData : function(){

        var vm = this;
        var schemeId = "";
        vm.suspendCalculatFlag = "0";

        this.$http.post('/iepsapi/ea/algorithm/queryCalculateInfo?schemeId='+schemeId+"&tableName=hp_algorithm_info&projectId="+this.projectId).then((res) => {
          var result = res.data.data;
          if(result){
            vm.form = result;

            if(vm.form.calculationStatus == "1"){
              vm.calculationStatusLabel = '计算中';
              vm.buttonText = '刷新';

              var calculationStartTime = vm.form.calculationStartTime;
              var calculationEndTime = vm.form.calculationEndTime;

              // 创建两个Date对象
              var startTime = new Date(calculationStartTime);
              var endTime;
              if(calculationEndTime&&calculationEndTime!="-"){
                endTime = new Date(calculationEndTime);
              }else{
                endTime = new Date();
              }
              
              // 计算两个时间点之间的差值（毫秒）
              var diff = endTime - startTime;
              
              // 将毫秒转换为分钟
              var minutes = Math.floor(diff / (1000 * 60));

              if(minutes>=5){
                vm.suspendCalculatFlag = "1";
                this.$message({
                  message: '当前算法计算时间超过五分钟，请中止计算，校核输入！',
                  type: 'info'
                });
              }else{
                vm.suspendCalculatFlag = "0";
              }
              

            }else if(vm.form.calculationStatus == "2"){
              vm.calculationStatusLabel = '已完成';
              vm.buttonText = '查看结果';
            }else if(vm.form.calculationStatus == "0"){
              vm.calculationStatusLabel = '未计算';
              vm.form.calculationStartTime = "-";
              vm.form.calculationEndTime = "-";
              vm.form.calculationTime = "-";
              vm.buttonText = '计算';
            }

          }else{
            vm.calculationStatusLabel = '未计算';
            vm.form = {
              id: '',
              calculationStatus: '0',
              calculationStartTime: '-',
              calculationEndTime: '-',
              calculationTime: '-',
            };

          }
        }).catch((error) => {
          console.log(error);
        });


      }, 
      buttonClick : function(){
        if(this.form.calculationStatus == "0"){
          this.executeAlgorithm();
        }else if(this.form.calculationStatus == "1"){
          this.queryPageData();
        }else{
          this.$parent.buttonClick('costBreakdown', 'hpDiv6');
        }
      },

      suspendCalculat: function(){
        
        var vm = this;

        this.$http.post('/iepsapi/ea/algorithm/suspendCalculat?algorithmType=hp'+""+"&tableName=hp_algorithm_info").then((res) => {
          vm.queryPageData();
        }).catch((error) => {
          console.log(error);
        });

      },

      executeAlgorithm : function(){

        const loading = this.$loading({
          lock: true,
          text: '调用算法中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        
        var vm = this;

        this.$http.post('/iepsapi/ea/algorithm/executeAlgorithmHydrogenProductionEconomic?projectId='+this.projectId+"&tableName=hp_algorithm_info").then((res) => {
   
          var result = res.data.data;
          if(result){

            loading.close();  
            
            vm.form = result;

            if(vm.form.calculationStatus == "1"){
              vm.calculationStatusLabel = '计算中';
              vm.buttonText = '刷新';
            }else if(vm.form.calculationStatus == "2"){
              vm.calculationStatusLabel = '已完成';
              vm.buttonText = '查看结果';
            }

          }else{
            vm.calculationStatusLabel = '未计算';
            vm.form = {
              id: '',
              calculationStatus: '0',
              calculationStartTime: '-',
              calculationEndTime: '-',
              calculationTime: '-',
            };

          }
        }).catch((error) => {
          console.log(error);
        });


      },
       
    }
  }
</script>

 
<style>

.v-modal {
      position: fixed;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      opacity: .5;
      background: #ffffff00;
  }
  
.el-table{
  background-color: rgb(20 34 78);
  color: #FFFFFF;
  border-color: rgb(24 63 124 / 51%);
  *{
    border-color: rgb(24 63 124 / 51%) !important;
  }
  .el-table__header-wrapper{
    tr{
      background-color: rgb(10 24 90 / 81%);
      th{
        color: #ffffff;
        background-color: rgba(0,0,0,0.4);
      }
    }
  }
  .el-table__body-wrapper{
    tr{
      background-color: rgb(8 26 80 / 88%);
      &:hover>td{      
        background-color: rgb(13 30 80);
      }
    }
  }
}
  .el-form--inline .el-form-item__content {
      display: inline-block;
      vertical-align: middle;
      margin-left: 1vh;
  }

  .el-radio {
      color: #eff4ff;
      cursor: pointer;
      margin-right: 30px;
  }

  .el-form-item {
      margin-bottom: 5px;
  }
  .el-form-item__label {
      text-align: right;
      vertical-align: middle;
      float: left;
      font-size: 14px;
      color: #f2f2f2;
      line-height: 40px;
      padding: 0 1px 0 0;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
  }
  .el-input__inner { 
      background-color: #202b6180;
      background-image: none;
      border-radius: 4px;
      border: 1px solid #5c7bc5;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      color: #a3aec5;
      display: inline-block;
      font-size: inherit;
      height: 40px;
      line-height: 40px;
      outline: 0;
      padding: 0 15px;
      -webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);
      transition: border-color .2s cubic-bezier(.645,.045,.355,1);
      width: 100%;
  }

  .el-input-group__append, .el-input-group__prepend {
    background-color: #305997;
    color: #ffffff;
    vertical-align: middle;
    display: table-cell;
    position: relative;
    border: 1px solid #506aa7;
    border-radius: 4px;
    padding: 0 20px;
    width: 1px;
    white-space: nowrap;
  }
</style>


<style scoped> 

/deep/ .v-modal {
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        opacity: .5;
        background: #ffffff00;
    }

/deep/ .el-dialog {
    background:rgba(40,54,121, 0.9);
    -webkit-box-sizing: border-box;
    border: none;

  }
  /deep/ .el-dialog__header {
    padding: 20px 20px 10px;
    color: #FFF;
    background:#374590;
  }
  /deep/ .el-dialog__title {
    line-height: 15px;
    font-size: 16px;
    color: #FFF;
  }
  /deep/ .el-form-item__label {
    text-align: right;
    vertical-align: middle;
    float: left;
    font-size: 14px;
    color: #FFF !important;
    line-height: 40px;
    padding: 0 12px 0 0;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  /deep/ .el-input.is-disabled .el-input__inner{
    background-color: #161E43;
    border-color: #161E43;
    color: #FFFFFF;
    text-align: left;
    cursor: not-allowed;
  } 



  /deep/ .el-input__inner { 
      background-color: #202b6180;
      background-image: none;
      border-radius: 4px;
      border: 1px solid #5c7bc5;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      color: #f2f2f2;
      display: inline-block;
      font-size: inherit;
      height: 31px;
      line-height: 31px;
      outline: 0;
      padding: 0 15px;
      -webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);
      transition: border-color .2s cubic-bezier(.645,.045,.355,1);
      width: 100%;
  }

  /deep/ .el-input-group__append, .el-input-group__prepend {
    background-color: #305997;
    color: #ffffff;
    vertical-align: middle;
    display: table-cell;
    position: relative;
    border: 1px solid #506aa7;
    border-radius: 4px;
    padding: 0 20px;
    width: 1px;
    white-space: nowrap;
  }


/deep/.el-input.is-disabled .el-input__inner {
    background-color: #0e2c58e3;
    border-color: #E4E7ED;
    color: #C0C4CC;
    cursor: not-allowed;
}


  /* 设定地图的大小 */
  #allmap{
    height: 350px;
    width: 100%;
  }

  .buttonStyle {
    height: 4vh;
    width: 27vh;
    display: inline-block;
    -webkit-border-radius: 60px;
    -moz-border-radius: 60px;
    cursor: pointer;
    border-radius: 10px;
    font-family: Arial;
    border: none;
    color: #ffffff;
    font-size: 1.6vh;
    background: #1f2c6c3b;
    border-bottom: 1px solid rgb(64, 158, 255);
    margin: 1.5vh 10px;
    text-align: center;
    line-height: 4vh;
    vertical-align: middle;
    text-decoration: none;
  }

  .box{ 
    width: 98%;
    height: 6vh;
    margin: auto;
    border-radius: 5px;
    margin: auto;
    /* background: #202b614f;
    border: 1px solid rgb(62 111 219 / 88%); */
  }

  .titleCss{
    font-size: 22px;
    font-family: inherit;
    font-weight: 500;
    line-height: 1.2;
    color: #f2f2f2;
  }
</style>
